﻿
@{
    ViewBag.Title = ViewBag.CurrentMenuName;
    Layout = "~/Views/Shared/_AdminLayout.cshtml";

    SelectList slState = new SelectList(new[] {
        new SelectListItem { Value = "2", Text = "Tất cả"},
        new SelectListItem { Value = "0", Text = "Đang sử dụng"},
        new SelectListItem { Value = "1", Text = "Không sử dụng"}
    }, "Value", "Text", 2);
}

<div class="content">
    <div class="clear"></div>
    <table style="width: 100%">
        <tr>
            <td style="height: 40px;">
                <div id="nav"><a href="/Users/Index" title="">@ViewBag.Title.ToUpper()</a></div>
            </td>
        </tr>
        <tr>
            <td style="">
                <div style="width: 90%;">
                    <div>
                        <div class="col-md-2" style="text-align:right;padding:4px;">
                            Tên đăng nhập
                        </div>
                        <div class="col-md-4">
                            <input type="text" class="txtsearch TextBoxContent" style="width: 100% !important" id="tai_khoan" name="username">
                        </div>

                        <div class="col-md-2" style="text-align: right; padding: 4px">
                            Họ và tên
                        </div>
                        <div class="col-md-4">
                            <input type="text" class="txtsearch TextBoxContent" style="width: 100% !important" id="ho_ten" name="fullname">
                        </div>
                        <div class="col-md-2" style="text-align:right;padding:4px;">
                            Trạng thái sử dụng
                        </div>
                        <div>
                            <div class="col-md-4">
                                @Html.DropDownList("Status", new SelectList(slState, "value", "text", 2),
                                        new { @id = "status", @name = "code", @class = "SelectBoxContent", @placeholder = "" })
                            </div>
                        </div>
                        <div class="col-md-2" style="text-align:right;padding:4px;">
                            Đơn vị
                        </div>
                        <div>
                            <div class="col-md-4">
                                @Html.DropDownList("DON_VI_ID_SEARCH", ViewBag.DanhSachDonVi as List<SelectListItem>,
                                                 new { @id = "DON_VI_ID_SEARCH", @name = "code", @class = "SelectBoxContent", @placeholder = "" })
                            </div>
                        </div>

                    </div>
                    <div>
                        <div style="height: 10px;"></div>
                        <div class="col-md-12">

                            <div style="padding-top: 10px; text-align: center">
                                <div class="col-md-12">
                                    <span class="input-group-btn">
                                        <button id="btnSearch" class="btn btn-primary" type="button"  style="margin-left:45px">
                                            <span class=" glyphicon glyphicon-search"></span> Tìm kiếm
                                        </button>
                                        <button type="button" id="btnAdd" style="margin-left: 14px;" class="btn btn-primary" value="Thêm mới"><span class="glyphicon glyphicon-plus"></span> Thêm người dùng</button>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </td>
        </tr>
        <tr>
            <td>
                <div id="user-list" style="margin-top:10px;">
                    @{
                        Html.RenderPartial("_NguoiDungList");
                    }
                </div>
            </td>
        </tr>

    </table>
</div>

<script type="text/javascript">
    var currentPageIndex = 0;
    $(document).ready(function () {

        $('#btnSearch').bind('click', function (event) {
            btnSearchClick();
        });

        $('#btnAdd').bind('click', function (event) {
            functionCreateOrUpdate("", "Thêm mới", "Tạo mới");
        });

        //$('#input-state').change(function () {
        //    OnCriteria(0);
        //});

        //$("#input-code", "#input-name").on("keypress", function (event) {
        //    if (event.which == 13) {
        //        OnCriteria(0);
        //    }
        //});
        headers['VerificationToken'] = '@GetAntiForgeryToken()';
    });

    var headers = {};

    function btnSearchClick() {
        OnCriteria(0);
    }

    function onUpdate(id) {
        functionCreateOrUpdate(id, "Cập nhật", "Cập nhật");
    }

    function functionCreateOrUpdate(id, titleForm, buttonTile) {
        $('#user-model-container').html("");
        $('#user-model-container')
              .load("/NguoiDung/CreatePopupUpdate", { id: id}, function () {
                  $(this).dialog({
                      draggable: true,
                      modal: true,
                      resizable: false,
                      width: 'auto',
                      title: titleForm,
                      minHeight: 75,
                      my: "center",
                      at: "center",
                      of: window,
                      buttons: [
                            {
                                text: "Lưu",
                                icons: {
                                    primary: "ui-icon-heart"
                                },
                                class: 'btn btn-primary',
                                click: function () {
                                    var form = $('#frmNguoiDung');
                                    if (!form.valid()) {
                                        return false;
                                    }
                                    
                                    $.ajax({
                                        type: "POST",
                                        url: '/NguoiDung/ActionSave',
                                        data: {
                                            NGUOI_DUNG_ID: $("#NGUOI_DUNG_ID").val(),
                                            HO_VA_TEN: $("#HO_VA_TEN").val(),
                                            DON_VI_ID: $("#DON_VI_ID").val(),
                                            TAI_KHOAN: $("#TAI_KHOAN").val(),
                                            DIEN_THOAI: $("#DIEN_THOAI").val()
                                        },
                                        dataType: 'json',//chú ý là form đặc biệt thì phải điền trường này
                                        headers: headers,
                                        success: function (response) {
                                            if (response != null && response.status == 200) {
                                                window.notice(response.result, window.notice_success);
                                                OnCriteria(currentPageIndex);
                                            }
                                            else {
                                                window.notice(response.result, window.notice_error);
                                            }
                                            $('#user-model-container').dialog("destroy");
                                        }
                                    });
                                }
                            },
                            {
                                text: "Đóng",
                                icons: {
                                    primary: "ui-icon-close"
                                },
                                class: 'btn btn-primary',
                                click: function () {
                                    //alert($('#user-form').valid());
                                    //alert(JSON.stringify(this));
                                    $(this).dialog("destroy");
                                }
                            }
                      ]
                  });
              });
    }
    
    @functions{
        public string GetAntiForgeryToken()
        {
            string cookieToken, formToken;
            AntiForgery.GetTokens(null, out cookieToken, out formToken);
            return cookieToken + "," + formToken;
        }
    }


    function OnCriteria(pageIndex) {
        currentPageIndex = pageIndex;
        var name = $('#ho_ten').val().trim();
        var account = $("#tai_khoan").val().trim();
        var status = $("#status").val().trim();
        var donViId = $("#DON_VI_ID_SEARCH").val();
        $.ajax({
            dataType: "html",
            type: "POST",
            url: '/NguoiDung/ActionSearch',
            //contentType: "application/json",
            data: {
                HO_VA_TEN: name,
                TAI_KHOAN: account,
                DELETED: status,
                DON_VI_ID: donViId,
                pageIndex: pageIndex
            },
            success: function (response) {
                var $catlist = $('#user-list');

                if ($catlist == null || $catlist.length == 0) {
                    window.notice('@*@Resources.Localization.MsgItemNotFound*@', window.notice_error);
                }
                else {
                    $catlist.html(response);
                }
            }
        });
        setTimeout(function () { $('#search_form').find('input:text:visible:first').focus(); }, 500);
    }
</script>

